const localData=localStorage.getItem('pz_v3pz')


const state=localData?localData.menu:{
  // 展开收起
  isCollapse:false,
  // 菜单列表
  selectMenuList:[],
  routerList:[],
  menuActive:'1-1'

}

const mutations={
  // 展开收起
  collapseMenu(state){
    state.isCollapse=!state.isCollapse
  },
  // 菜单列表
  addMenu(state,payload){
    // 对数据去重
    if(state.selectMenuList.findIndex(item=>item.path===payload.path)==-1){
      state.selectMenuList.push(payload)
    } 
  },
  closeMenu(state,payload){
    // 找到点击数据的索引
    const index=state.selectMenuList.findIndex(item=>item.path===payload.path)
    state.selectMenuList.splice(index,1)
  },
  dynamicMenu(state,payload){
    //通过glob导入文件
    const modules=import.meta.glob('../views/**/**/*.vue')
    function routerSet(router){
      //判断没有子菜单，拼接路由数据
      router.forEach(route => {
        if( !route.children){
          const url=`../views${route.meta.path}/index.vue`
          // 拿到获取的vue组件
          route.component=modules[url]

        }else{
          routerSet(route.children)
        }
        
      });
      
    }
    routerSet(payload)
    //拿到完整的路由数据
    state.routerList=payload

  },
  updateMenuActive(state,payload){
    state.menuActive=payload
  },

  
}

export default {
    state,
    mutations
}